package com.karst.webnotes.chapter04
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLRequest;
	
	import org.aswing.AsWingManager;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;

	internal class BitmapMaterialImgExample extends BasicView
	{
		public function BitmapMaterialImgExample()
		{
			super() ;
			AsWingManager.getStage().frameRate = 24 ;
			createImgLoader() ;
			startRendering() ;
		}
		
		private var imgLoader : Loader = null ;
		private var material : BitmapMaterial = null ;
		private var plane : Plane = null ;
		private function createView( $bmpData : BitmapData ) : void
		{
		
			if( material == null )
			{
				material = new BitmapMaterial( $bmpData ) ;
			}
			
			if( plane == null )
				plane = new Plane( material, 500, 500, 1, 1 ) ;
			plane.x = 0 ;
			plane.y = 0 ;
			scene.addChild( plane ) ;
		}

		private function createImgLoader() : void
		{
			//1. 声明并创建一个加载对象-搬运工
			if( imgLoader == null )
				imgLoader = new Loader() ;
			//3. 搬运工的. 完成加载情况的. 添加事件监听（事件类的. 完成事件目标，事件处理方法）。监听到目标达到，即处理
			imgLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, loadedHandler ) ;
			//2. 搬运（新 路径请求（“FD项目包路径/子路径/位图文件”）），或使用硬盘目录创建路径请求
			imgLoader.load( new URLRequest( "01.jpg" ) ) ;
		}
		
		private function loadedHandler( event : Event ) : void
		{
			createView( (imgLoader.content as Bitmap).bitmapData ) ; 
		}
		
		override protected function onRenderTick( event : Event = null ) : void
		{
			super.onRenderTick( event ) ;
			if( plane )
			{
				plane.localRotationY-- ;
			}
		}
	}
}